/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package op;

import in_out.ReadXLS;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 *
 * @author Jônatas
 */
public class CreateContaVoip {

    public ContaTelefonica createConta(File file) throws Exception {

        ContaTelefonica conta = new ContaTelefonica();
        conta.contaMensal = new ArrayList<EntradaAbstrata>();

        HSSFWorkbook wb = ReadXLS.readFile(file);

        HSSFSheet sheet = wb.getSheetAt(0);

        int i = 0;

        do {
            HSSFRow row = sheet.getRow(i++);

            if(row == null)
                continue;

            if(row.getPhysicalNumberOfCells() == 0)
                continue;

            HSSFCell cell = row.getCell(0);

            if(cell.getCellType() == HSSFCell.CELL_TYPE_BLANK)
                continue;

            String word = cell.getStringCellValue().trim();

            if (word.toUpperCase().startsWith("ID") || word.toUpperCase().startsWith("ORIGEM")) {
                break;
            }

        } while (true);

        System.err.println("AQUI" + sheet.getPhysicalNumberOfRows());

        for (; i < sheet.getPhysicalNumberOfRows(); i++) {

            HSSFRow row = sheet.getRow(i);

            if(row == null)
                continue;

            EntradaAbstrata entrada = QuebraStringVoip.quebraString(row);

            if(entrada == null) {
                continue;
            }

            conta.contaMensal.add(entrada);

        }
        
        Collections.sort(conta.contaMensal, new ComparatorEntradaAbstrataPorTelefone());

        conta.contaResumida();

        return conta;
    }
}
